// 正式环境 
// const host = 'http://guaguaweixin.natapp1.cc';
// const host = 'http://localhost:8085';
const host = 'https://expressway.shanzhonglei.com';
// const httpUrl = `${host}/highVehicleApp`;

// const appId = uni.getAccountInfoSync().miniProgram.appId;

function request(url, data = {}, method = "GET", showMsg = false, ContentType = 'application/json') {
	return new Promise(function(resolve, reject) {
		resolve(requestBase(url, data, method, showMsg, ContentType));
	})  
}  

const requestBase = (url, data = {}, method = "GET", showMsg, ContentType = 'application/json') => {
	return new Promise(function(resolve, reject) {
		uni.showLoading({  
			title: '加载中...', 
			mask: true
		})  
		uni.request({
			url,
			data,
			method,
			header: {
				'Content-Type': ContentType, 
				// 'wxAppId': appId,
				'from': 'guaguaApp',
				'secret': uni.getStorageSync('secret'),
				'token': uni.getStorageSync('token')
			},
			success: function(res) {
				uni.hideLoading()
				let {code} = res.data;
				if (code === 10001) {
					if (showMsg) {
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							resolve(res.data)
						}, 1000)
					} else {
						resolve(res.data)
					}
				} else if (code === -1) {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 2500
					})
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 2500
					})
					reject(res.data);
				}   
			},
			fail: function(err) {
				uni.hideLoading()
				uni.showToast({
					title: '加载失败，请退出后重试！',
					icon: 'none',
					duration: 2000
				})
			},
			complete: function() {}
		})
	})
}

export {
	host,
	request
}
